export type UseNamespace = {
  b: (blockSuffix?: string) => string;
  e: (el: string) => string;
  m: (mo: string) => string;
  em: (el: string, mo: string) => string;
  bem: (blockSuffix: string, el: string, mo: string) => string;
  is: (name: string, ...args: [boolean | undefined] | []) => string;
};

function createBem(namespace: string, blockSuffix?: string, element?: string, modifier?: string): string {
  let cls = namespace;
  if (blockSuffix) {
    cls += `-${blockSuffix}`;
  }
  if (element) {
    cls += `__${element}`;
  }
  if (modifier) {
    cls += `--${modifier}`;
  }
  return cls;
}

export function useNamespace(block: string, needDot = false): UseNamespace {
  const defaultNamespace = 'fs';
  const namespace = needDot ? `.${defaultNamespace}-${block}` : `${defaultNamespace}-${block}`;
  const statePrefix = 'is-';
  const b = (blockSuffix: string = '') => createBem(namespace, blockSuffix);
  const e = (element: string) => (element ? createBem(namespace, '', element) : '');
  const m = (modifier: string) => (modifier ? createBem(namespace, '', '', modifier) : '');
  const em = (element: string, modifier: string) => (element && modifier ? createBem(namespace, '', element, modifier) : '');
  const bem = (blockSuffix: string, element: string, modifier: string) => (element && modifier && blockSuffix ? createBem(namespace, blockSuffix, element, modifier) : '');
  const is = (name: string, ...args: [boolean | undefined] | []) => {
    const state = args.length >= 1 ? args[0]! : true;
    return name && state ? `${statePrefix}${name}` : '';
  };
  return {
    b,
    e,
    m,
    em,
    bem,
    is,
  };
}
